package com.leyou.search.web;

import com.leyou.search.dto.SearchRequest;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 自动补全
     * @param key 用户输入的搜索条件
     * @return 自动补全的信息
     */
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String key) {
        return searchService.getSuggest(key);
    }

    /**
     * 根据搜索条件，查询到商品集合
     * @param searchRequest 搜索条件
     * @return 分页显示的商品集合
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> listData(@RequestBody SearchRequest searchRequest) {
        return searchService.listData(searchRequest);
    }

    /**
     * 根据用户输入的条件，返回过滤条件
     * @param searchRequest 用户输入的条件
     * @return 过滤条件
     */
    @PostMapping("/filter")
    public Mono<Map<String, List<? extends Object>>> listFilter(@RequestBody SearchRequest searchRequest) {
        return searchService.listFilter(searchRequest);
    }
}
